package com.dc.refactoring._3_重新组织数据._4_ChangeReferenceToValue;

import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

/**
 * Description: 将引用对象改为值对象
 * @author: gdc
 * @date: 2020/11/16
 * @version 1.0
 */
// 这个类所做的就是保存并返回一个货币种类代码
@Slf4j
public class Currency {
    private String code;

    public Currency(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    // 重新定义 equals()、hashCode()方法
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Currency currency = (Currency) o;
        // 此处比较了 code 是否一致
        return Objects.equals(code, currency.code);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code);
    }

    public static void main(String[] args) {
        String code = "CUD";
        log.info("比较结果为：{}", new Currency(code).equals(new Currency(code)));
    }
}
